<template>
  <div class="login-container">
    <!-- <TestInfo /> -->
    <div class="login-card">
      <div class="login-header">
        <h1>登录</h1>
        <p>欢迎回到待办日历</p>
      </div>
      
      <n-form
        ref="formRef"
        :model="form"
        :rules="rules"
        size="large"
        @submit.prevent="handleLogin"
      >
        <n-form-item path="username" label="用户名">
          <n-input
            v-model:value="form.username"
            placeholder="请输入用户名或邮箱"
            :input-props="{ autocomplete: 'username' }"
          />
        </n-form-item>
        
        <n-form-item path="password" label="密码">
          <n-input
            v-model:value="form.password"
            type="password"
            placeholder="请输入密码"
            show-password-on="mousedown"
            :input-props="{ autocomplete: 'current-password' }"
          />
        </n-form-item>
        
        <n-form-item>
          <n-button
            type="primary"
            size="large"
            :loading="loading"
            :disabled="!canSubmit"
            attr-type="submit"
            block
          >
            登录
          </n-button>
        </n-form-item>
      </n-form>

      <div class="guest-login">
        <n-button
          size="large"
          :loading="guestLoading"
          @click="handleGuestLogin"
          block
          secondary
        >
          马上体验
        </n-button>
        <p class="guest-hint">无需注册，快速体验应用功能</p>
      </div>

      <div class="login-footer">
        <p>
          还没有账号？
          <router-link to="/register" class="link">立即注册</router-link>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInst, FormRules } from 'naive-ui'
import type { LoginForm } from '@/types'
import { useUserStore } from '@/stores/user'
// import TestInfo from '@/components/TestInfo.vue'

const router = useRouter()
const userStore = useUserStore()

// 表单引用
const formRef = ref<FormInst | null>(null)

// 表单数据
const form = reactive<LoginForm>({
  username: '',
  password: ''
})

// 加载状态
const loading = ref(false)
const guestLoading = ref(false)

// 表单验证规则
const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名或邮箱', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 是否可以提交
const canSubmit = computed(() => {
  return form.username.trim() && form.password.trim() && !loading.value
})

// 处理登录
const handleLogin = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true

    const success = await userStore.login(form)
    if (success) {
      router.push('/home')
    }
  } catch (error) {
    console.error('Login validation error:', error)
  } finally {
    loading.value = false
  }
}

// 处理匿名登录
const handleGuestLogin = async () => {
  guestLoading.value = true
  try {
    const success = await userStore.loginAsGuest()
    if (success) {
      router.push('/home')
    }
  } catch (error) {
    console.error('Guest login error:', error)
  } finally {
    guestLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 40px 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.guest-login {
  margin-top: 16px;
  text-align: center;
}

.guest-hint {
  margin-top: 8px;
  color: #999;
  font-size: 12px;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.login-footer p {
  color: #666;
  font-size: 14px;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .login-card {
    padding: 30px 20px;
  }

  .login-header h1 {
    font-size: 24px;
  }
}
</style>
